package org.flint92.springboot.mybatis.quickstart;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.flint92.springboot.mybatis.domain.User;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.EncodedResource;

import java.io.IOException;
import java.io.Reader;
import java.util.Properties;

/**
 * @author flint92
 */
public class MybatisXmlConfigurationDemo {

    public static void main(String[] args) throws IOException {

        ResourceLoader loader = new DefaultResourceLoader();

        Resource resource = loader.getResource("classpath:/mybatis/mybatis-config.xml");

        EncodedResource encodedResource = new EncodedResource(resource, "utf-8");

        Reader reader = encodedResource.getReader();

        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();

        SqlSessionFactory factory = builder.build(reader, "dev", new Properties());

        try (
                SqlSession sqlSession = factory.openSession()
        ) {

            String statement = "org.flint92.springboot.mybatis.mapper.UserMapper.selectUserById";

            // select user by id
            User user = sqlSession.selectOne(statement, 1);

            System.out.println(user);
        }


    }

}
